const wx2my = require('../../../wx2my');
const Behavior = require('../../../Behavior');
var util = require("../../utils/util.js"),
  app = getApp();
var base = require('./../../../config.js');
Page({
  data: {
    users: !0,
    level_name: "初始会员",
    score: 0,
    imgUrl: '',
    user_info: ''
  },

  gotoMember() {
    let that = this;
    // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
    my.navigateToMiniService({
      serviceId: "2019072365974237", // 插件id,固定值勿改
      servicePage: "pages/hz-enjoy/main/index", // 插件⻚面地址,固定值勿改
      extraData: {
        "alipay.huabei.hz-enjoy.templateId": "2019082600020903160000060462",
        "alipay.huabei.hz-enjoy.partnerId": "2088421609886161",
        "alipay.huabei.hz-enjoy.userId": that.data.user_info.list.user_id,
      },
      success: (res) => {
        console.log(JSON.stringify(res));
        // my.alert({
        //   title: JSON.stringify(res) + "1"
        // });
      },
      fail: (res) => {
        console.log(JSON.stringify(res));

        // my.alert({
        //   title: JSON.stringify(res) + '2'
        // });
      },
      complete: (res) => {
        console.log(JSON.stringify(res));
        // my.alert({
        //   title: JSON.stringify(res) + "3"
        // });
      },
    });


  },

  onLoad: function(e) {
    var t = this;

    // app.getUserInfo(function(g) {
    //   console.log('logload.....')
    //   console.log(g);
    //   wx2my.setStorageSync("userInfo", g.data)
    //   t.setData({
    //     user_info: g.data
    //   })
    //   console.log(g.data);
    // })
    // console.log(g.data)


    wx2my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    });

    var n = wx2my.getStorageSync(["imgUrl1"]).data;
    t.setData({ imgUrl: n });


    wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'GetSystem',
        m: 'zh_jdgjb',
      },
      success: (e) => {
        console.log('GetSystem请求成功')
        console.log(e)
        console.log(e);
        wx2my.setStorageSync("platform", e.data)
        wx2my.setNavigationBarColor({
          frontColor: '#000000',
          backgroundColor: '#ffffff'
        })
        wx2my.setNavigationBarTitle({
          title: e.data.pt_name
        })
        t.setData({
          platform: e.data,
          color: e.data.color,
          grade: e.data.open_member
        });
      },
      fail: (err) => {
        console.log(err)
      }
    })


  },

  bind_user_info: function(e) {
    var that = this;
    app.getUserInfo(function(g) {
      console.log('logload.....')
      console.log(g);
      if (g.data.access_token == "") {
        console.log("这是没有检测到授权");
        that.setData({
          user_info: [],
         
        })
      } else {
        that.setData({
          user_info: g.data,
         
        })
      }
      wx2my.setStorageSync("userInfo", g.data)

    })
   
  },
  formSubmit: function(t) {
    var e = this;
    console.log(t);
    var a = t.detail.formId,
      o = e.data.grade,
      n = e.data.users;
    console.log(n);
    if ("" == n.img) e.setData({
      user_info: !0
    }), app.getUserInfo(function(t) { }); else if (n.tel == '') {
      wx2my.showModal({
        content: "请先绑定手机号",
        success: function(t) {
          t.confirm && (console.log("用户点击确定"), wx2my.navigateTo({
            url: "../register/register"
          }));
        }
      });
    } else {// 授权成功
    }
  },

  // user: function(e) {
  //   var n = this;
  //   app.getUserInfo(function(e) {
  //     console.log(e);
  //     if (0 == e.level_id) var t = "初始会员"; else t = e.level_name;
  //     console.log(t), n.setData({
  //       level_name: t,
  //       score: e.score,
  //       userInfo: e
  //     });
  //   });
  // },

  my_interests() {
    // 我的权益卡
    wx2my.navigateTo({
      url: './../interests/interests_list'
    });
  },

  refresh: function(e) {
    var t = this,
      n = getCurrentPages(),
      o = n[n.length - 1],
      a = n[n.length - 2];
    console.log(n)
    console.log(o)
    console.log(a);
    var s = wx2my.getStorageSync(["userInfo"]).data.uid;
    // console.log(s);
    wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'CheckRz',
        m: 'zh_jdgjb',
        user_id: s
      },
      success: (res) => {
        // console.log('CheckRz请求成功')
        // console.log(res)
        t.setData({
          state: res.data
        });
      },
      fail: (err) => {
        console.log(err)
      }
    })

   

    wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'GetFxSet',
        m: 'zh_jdgjb',
      },
      success: (res) => {
        // console.log('GetFxSet请求成功')
        // console.log(res)
        t.setData({
          GetFxSet: res.data
        });
      },
      fail: (err) => {
        console.log(err)
      }
    })

   
  },
  mine_order: function(e) {
    if (1 == this.data.users) {
      var t = e.currentTarget.dataset.index,
        n = t;
      console.log(t), wx2my.navigateTo({
        url: "../order/order?activeIndex=" + n + "&index=" + t,
        success: function(e) { },
        fail: function(e) { },
        complete: function(e) { }
      });
    } else this.bind_user_info();
  },
  home: function(e) {
    wx2my.reLaunch({
      url: "../index/index_c"
    });
  },

  // 跳转到评价中心
  evaluate: function() {
    wx2my.navigateTo({
      url: './evaluate'
    });
  },

  seller_manage: function(e) {
    1 == this.data.users ? wx2my.navigateTo({
      url: "../login_entry/login_entry"
    }) : this.bind_user_info();
  },
  opensetting: function(e) {
    wx2my.openSetting({});
  },
  integral: function(e) {
    1 == this.data.users ? wx2my.navigateTo({
      url: "../jifen/jifen"
    }) : this.bind_user_info();
  },
  settled_store: function(e) {
    1 == this.data.users ? wx2my.navigateTo({
      url: "../settled_store/settled_store"
    }) : this.bind_user_info();
  },
  // 拨打电话
  service: function(e) {
    console.log(wx2my.getStorageSync(["platform"]).data)
    var n = wx2my.getStorageSync(["platform"]).data.tel;
    console.log(n);
    wx2my.makePhoneCall({
      phoneNumber: n
    });
    // 1 == this.data.users ? wx2my.navigateTo({
    //   url: "../service/service"
    // }) : this.bind_user_info();
  },
  // 极速开票
  kp: function() {
    my.navigateToMiniService({
      serviceId: '2019051464537454', // 插件id 2019051464537454
      extraData: {
        "plugin.common.customParams": {
          "outMerchantId": "UYQFCY" // 传入的六位代码
        }
        //'plugin.common.customParams.outMerchantId' : "UYQFCY"
      },
      success: (res) => {
        console.log('succ', res);
      },
      fail: (res) => {
        console.log('fail', res);
      },
      complete: (res) => {
        console.log('complete', res);
      }
    });
  },
  coupon: function(e) {
    1 == this.data.users ? wx2my.navigateTo({
      url: "../coupon/store_coupon"
    }) : this.bind_user_info();
  },
  my_tickets: function(e) {
    1 == this.data.users ? wx2my.navigateTo({
      url: "../coupon/my_tickets"
    }) : this.bind_user_info();
  },
  receive_coupon: function(e) {
    1 == this.data.users ? wx2my.navigateTo({
      url: "../coupon/receive_coupon"
    }) : this.bind_user_info();
  },
  user_zhuce: function(e) {
    var t = this.data,
      n = t.userInfo,
      o = t.grade;
    console.log(o), 1 == o && null != o ? (console.log("可以注册"), "" == n.zs_name || null == n.zs_name ? (console.log("检测到用户尚未注册会员"), wx2my.navigateTo({
      url: "../register/register"
    })) : (console.log("检测到用户已经注册会员"), wx2my.navigateTo({
      url: "member"
    }))) : wx2my.navigateTo({
      url: "member"
    });
  },
  cancel_storange: function(e) {
    wx2my.clearStorage(), wx2my.showToast({
      title: "清除成功"
    });
  },
  copyright: function(e) {
    var t = wx2my.getStorageSync("platform").tz_appid;
    wx2my.navigateToMiniProgram({
      appId: t,
      success: function(e) {
        console.log(e);
      }
    });
  },
  recharge: function(e) {
    1 == this.data.users ? wx2my.navigateTo({
      url: "../recharge/index"
    }) : this.bind_user_info();
  },
  score_detail: function(e) {
    1 == this.data.users ? wx2my.navigateTo({
      url: "../jifen/scoredetails"
    }) : this.bind_user_info();
  },
  distribution: function(e) {
    var t = this;

    if (1 == t.data.users) {
      var n = t.data.GetFxSet;
      console.log(n);
      var o = t.data.userInfo,
        a = wx2my.getStorageSync(["userInfo"]).data.id,
        s = t.data.grade;
      "" == o.zs_name && 1 == s ? wx2my.showModal({
        content: "请先绑定手机号",
        success: function(e) {
          e.confirm && (console.log("用户点击确定"), wx2my.navigateTo({
            url: "../register/register"
          }));
        }
      }) : app.util.request({
        url: "entry/wxapp/MyDistribution",
        data: {
          user_id: a
        },
        success: function(e) {
          console.log(e), 0 == e.data ? wx2my.navigateTo({
            url: "../distribution/examine"
          }) : 1 == e.data.state ? wx2my.showModal({
            title: "温馨提示",
            content: "系统正在审核中，请稍后再试"
          }) : 2 == e.data.state ? wx2my.navigateTo({
            url: "../distribution/distribution"
          }) : 3 == e.data.state && wx2my.showModal({
            title: "温馨提示",
            content: "您的申请已被拒绝"
          });
        }
      });
    } else t.bind_user_info();
  },

  

  onReady: function() { },

  onShow: function() {
    var that = this;
    that.bind_user_info();
    // app.getUserInfo(function(g) {
    //   console.log('logload.....')
    //   console.log(g)
    //   wx2my.setStorageSync("userInfo", g.data)
    //   that.setData({
    //     user_info: g.data
    //   })
    // })
    this.setData({
      user_info: wx2my.getStorageSync(['userInfo']).data
    })
    this.refresh()


    wx2my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    });
  },

  onHide: function() { },
  onUnload: function() { },
  onPullDownRefresh: function() { },
  onReachBottom: function() { }
});